package com.douwong.student.task;

import android.os.AsyncTask;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.contant.NetConfig;
import com.douwong.student.interfaces.WorkResultNotify;
import com.douwong.student.model.FeaturesBean;
import com.douwong.student.model.PrepareBean;
import com.douwong.student.model.RequestAction;
import com.douwong.student.model.StatusCode;
import com.douwong.student.utils.NetUtils;
import com.douwong.student.utils.UIUtils;

import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * 导读列表查询
 * 
 * @author Administrator
 * 
 */
public class FeatureTaskList extends AsyncTask<String, Integer, Integer> {

	private ArrayList<FeaturesBean> listData = null;
	private WorkResultNotify<FeaturesBean> workResult = null;
	public static int NO_DATA = 0;// 没有新的数据
	public static int REQUESTOK = 1;// 加载更多
	public static int NO_NETWOK = -1;// 没有网络
	private RequestAction action;
	private BaseApplication app;
	
	private String key;
	private int type=1;//1导读 2 任务

	public FeatureTaskList(RequestAction action, BaseApplication app) {
		this.app = app;
		this.action = action;
	}

	public void setWorkStautsListener(WorkResultNotify<FeaturesBean> workResult) {
		this.workResult = workResult;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected Integer doInBackground(String... params) {
		int resultCode = REQUESTOK;
		key = BaseApplication.sUserBean.getUserbean().getEuNumber() + "_features_"
				+ params[0];
		if (RequestAction.INIT == action) {
			if (app.readObject(key) != null) {
				listData = (ArrayList<FeaturesBean>) app.readObject(key);
				return resultCode;
			} else if (NetUtils.NetwrokIsUseful(UIUtils.getContext())) {
				resultCode = accessNetworkRequestData(params);
			} else {
				resultCode = NO_NETWOK;
			}
		} else if (NetUtils.NetwrokIsUseful(UIUtils.getContext())) {
			resultCode = accessNetworkRequestData(params);
		} else {
			resultCode = NO_NETWOK;
		}

		return resultCode;
	}

	private int accessNetworkRequestData(String... params) {
		String response = null;
		int resultCode = REQUESTOK;
		List<BasicNameValuePair> arrayParams = new ArrayList<BasicNameValuePair>();
		arrayParams.add(new BasicNameValuePair("classId", BaseApplication.sUserBean
				.getPersonalbean().getClassId()+""));
		Log.e("classId",BaseApplication.sUserBean
				.getPersonalbean().getClassId()+"");
//		arrayParams.add(new BasicNameValuePair("plType", "3"));
//		arrayParams.add(new BasicNameValuePair("leType", "2"));//leType=(1备课，2 导读，3资源，4测验， 5任务)
		arrayParams.add(new BasicNameValuePair("rows", "30"));
		arrayParams.add(new BasicNameValuePair("page", params[0]));
//		arrayParams.add(new BasicNameValuePair("sourceType ", "5"));
		try {
			response = NetUtils
					.httpGet(NetConfig.PREPARE_INDRO_LIST, arrayParams);
			Log.e("response", response + "");
			StatusCode<PrepareBean> sc = (StatusCode<PrepareBean>) JSON
					.parseObject(response,
							new TypeReference<StatusCode<PrepareBean>>() {
							});
			if (sc.isFlag()) {
				Log.e("sc", sc.getData().getRows() + "");
				if ((sc.getData().getRows() == null || sc.getData().getRows()
						.equals("")||sc.getData().getRows().equals("[]"))&&action==RequestAction.REFRESH) {
					app.saveObject(listData, key);
					return NO_DATA;
				}	if ((sc.getData().getRows() == null || sc.getData().getRows()
						.equals("")||sc.getData().getRows().equals("[]"))&&action==RequestAction.LOADMROE) {
					app.saveObject(listData, key);
					return NO_DATA;
				}
				listData = (ArrayList<FeaturesBean>) JSON.parseArray(sc
						.getData().getRows(), FeaturesBean.class);
				if(action==RequestAction.REFRESH){
					app.saveObject(listData, key);
				}else if(action==RequestAction.INIT){
					app.saveObject(listData, key);
				}else {
					app.saveObject(listData, key);
				}
				Log.e("listData", listData.size() + "");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultCode;
	}

	@Override
	protected void onPostExecute(Integer result) {
		super.onPostExecute(result);
		workResult.notify(result, listData);
	}

}
